package com.example.shop.controller;

import com.example.shop.bean.CollectBean;
import com.example.shop.bean.FollowBean;
import com.example.shop.mapper.FollowMapper;
import com.example.shop.util.NotNullUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/follow")
public class FollowController {
    @Autowired
    FollowMapper followMapper;
    //增加
    @ResponseBody
    @RequestMapping("/add")
    public String addCollect(FollowBean bean){
        List<FollowBean> followL;
        followL=followMapper.listByUidPid(bean.uid,bean.sid);
        if(followL.size()<=0){
            try{
                followMapper.insert(bean);
                return "关注成功!";
            }catch (Exception e){
                System.out.println(e);
                System.out.println("关注卖家失败");
                return "关注失败!";
            }
        }else{
            return "已经关注过了!";
        }
    }
    //取消关注
    @ResponseBody
    @RequestMapping("/cancel")
    public String cancelfollow(FollowBean bean){
        try{
            followMapper.cancelFollowByUidPid(bean.uid,bean.sid);
            return "已取消关注";
        }catch (Exception e){
            System.out.println("取消关注失败");
            return "取消关注失败";
        }
    }

    //判断是否关注
    @ResponseBody
    @RequestMapping("/iffollowed")
    public String ifFollowed(FollowBean bean){
        List<FollowBean> followL;
        followL=followMapper.listByUidPid(bean.uid,bean.sid);
        if(followL.size()>0){
            return "1";
        }else{
            return "0";
        }
    }

    @ResponseBody
    @RequestMapping("/list")
    public List<FollowBean> followList(Integer uid){
        if(uid==null){
            return null;
        }
        List<FollowBean> followL;
        followL=followMapper.followList(uid);
        return followL;
    }

    @ResponseBody
    @RequestMapping("/list2")
    public List<FollowBean> followList2(){

        List<FollowBean> followL;
        followL=followMapper.followList2();
        return followL;
    }
}
